package com.weiawei.ui;


import com.weiawei.component.BackGroundPannel;
import com.weiawei.util.PathUtils;
import com.weiawei.util.ScreenUtils;
import com.weiawei.util.shujuqidongUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class RegisterInterface {

    final int width = 1000;
    final int height = 600;
    JFrame jf = new JFrame("注册登录页面");

    shujuqidongUtils shuju2 = new shujuqidongUtils();




    public void init() throws IOException {

        shuju2.chushihua();

        // 设置窗口的属性
        // 设置窗口相关的属性 位置+长宽高
        jf.setBounds((ScreenUtils.getScreenWidth() - width) / 2, (ScreenUtils.getScreenHeight() - height) / 2, width, height);
        // 窗口大小不可变
        jf.setResizable(false);
        // 设定窗口图标
        jf.setIconImage(ImageIO.read(new File(PathUtils.iconpath)));

        jf.setVisible(false);

        //设置背景图片
        BackGroundPannel bgpanel = new BackGroundPannel(ImageIO.read(new File(PathUtils.zhucepath)));

        bgpanel.setBounds(0,0,width,height);

        Box vbox = Box.createVerticalBox();

        //组装密码
        Box ubox = Box.createHorizontalBox();
        JLabel ulabel = new JLabel("用户名：");
        JTextField uField = new JTextField(15);
        uField.setToolTipText("请输入15位数字、字母以内的账号");

        ubox.add(ulabel);
        ubox.add(Box.createHorizontalStrut(20));
        ubox.add(uField);

        // 组装密码
        Box Pbox = Box.createHorizontalBox();
        JLabel Plabel = new JLabel("密码   ：");
        JTextField PField = new JTextField(15);
        PField.setToolTipText("请输入15位数字、字母以内的账号");

        Pbox.add(Plabel);
        Pbox.add(Box.createHorizontalStrut(20));
        Pbox.add(PField);

        // 组装按钮
        Box btnbox = Box.createHorizontalBox();
        JButton registbtn = new JButton("注册");
        JButton backbtn = new JButton("返回主界面");

        btnbox.add(registbtn);
        btnbox.add(Box.createHorizontalStrut(80));
        btnbox.add(backbtn);

        registbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = uField.getText().trim();
                String password = PField.getText().trim();

                int flag = shuju2.regist(name,password);

                if(flag == 1) {
                    JOptionPane.showMessageDialog(jf,"您输入的账号已存在，请重新输入");
                }else
                {
                    JOptionPane.showMessageDialog(jf,"恭喜您注册成功！！！");
                    JOptionPane.showMessageDialog(jf,"即将返回注册页面");
                    try {
                        new MainInterface().init();
                        jf.dispose();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }

            }
        });

        backbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new MainInterface().init();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                jf.dispose();
            }
        });


        //整合组装
        vbox.add(Box.createVerticalStrut(250));
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(Pbox);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(btnbox);

        bgpanel.add(vbox);

        jf.add(bgpanel);


        jf.setVisible(true);

    }

    public static void main(String[] args) {

        try {
            new RegisterInterface().init();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
